#导入相应的库
import os
from config import config
import data_get
import parase_content
import data_save
#保存图片
def save_img(content,img_name):
    with open(img_name,"wb")as f:
        f.write(content)
        f.close()
#创建文件夹
def create_new_file(file_name):
    try:
        os.mkdir(config.img_file+file_name)
    except FileExistsError:
        print("文件夹已存在")

def main():
    pic_name = input("请输入要抓取的内容:")
    #创建文件夹
    create_new_file(pic_name)
    #定义初始化图片名
    pic_num = 0
    #初始化图片页数
    pic_page = 0

    for i in range(3):
        print("************正在抓取第 " + str(i + 1) + " 页的图片******************")
        # 引用加载头
        pic_page = pic_page + 1
        url, headers, params = config.pic_params(str(pic_page * 30), pic_name)
        print(url)
        # 获取图片源代码
        data = (url, headers, params)
        # print(data)
        # 获取图片链接
        regular = r'"thumbURL":"(.*?)"'
        img_url = parase_content.parase_content(regular,data)

        for img in img_url:
            pic_num = pic_num + 1
            print(pic_num)
            img_content = data_get.get_content_no_headers(img)
            img_name = config.img_file + pic_name + "/" + str(pic_num) + ".jpg"
            data_save.save_img(img_content, img_name)
if __name__ == "__main__":
    main()